package com.iot.manage.dao;

import com.alibaba.fastjson.JSONObject;
import com.iot.manage.pojo.App;
import com.iot.manage.pojo.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface UserMapper {

    @Select("<script>" +
            "SELECT\n" +
            "temp.USER_ID userId,\n" +
            "temp.LOGIN_NAME loginName,\n" +
            "temp.LOGIN_PASS loginPass,\n" +
            "temp.CODE_NAME userAccess,\n" +
            "temp.CODE_DESC codeDesc\n" +
            "FROM (\n" +
            "SELECT\n" +
            "ROWNUM AS rowno,\n" +
            "t.*,\n" +
            "c.*\n" +
            "FROM T_USER t\n" +
            "LEFT JOIN (SELECT * FROM T_SYS_CODE WHERE CODE_TYPE='用户权限') c ON t.USER_ACCESS = c.CODE_VALUE\n" +
            "\tWHERE t.LOGIN_NAME LIKE CONCAT(CONCAT('%', #{query}),'%') AND ROWNUM &lt;= #{pagesize}\n" +
            "\t) temp\n" +
            "WHERE temp.rowno &gt; #{start}" +
            "</script>")
    List<User> findUsersInfo(@Param("query") String query,
                             @Param("start") int start,
                             @Param("pagesize") int pagesize);

    @Select("<script>SELECT COUNT(a.USER_ID) FROM T_USER a WHERE a.LOGIN_NAME LIKE CONCAT(CONCAT('%', #{query}),'%')</script>")
    int countUsersInfo(String query);

    @Delete("<script>DELETE FROM T_USER WHERE USER_ID = #{id}</script>")
    void deleteUserInfo(String id);

    @Insert("<script>INSERT INTO T_USER (USER_ID,LOGIN_NAME,LOGIN_PASS,USER_ACCESS) VALUES (#{userId},#{loginName},#{loginPassMD5},#{userAccess})</script>")
    void addUserInfo(JSONObject params);

    @Select("<script>" +
            "SELECT\n" +
            "\ta.USER_ID userId,\n" +
            "\ta.LOGIN_NAME loginName,\n" +
            "\ta.LOGIN_PASS loginPass,\n" +
            "\tc.CODE_NAME userAccess,\n" +
            "\tc.CODE_VALUE codeValue\n" +
            "FROM T_USER a \n" +
            "LEFT JOIN (SELECT * FROM T_SYS_CODE WHERE CODE_TYPE='用户权限') c ON a.USER_ACCESS = c.CODE_VALUE\n" +
            "WHERE a.USER_ID = #{param} OR a.LOGIN_NAME = #{param}" +
            "</script>")
    User findUserInfo(String id);

    @Select("<script>" +
            "UPDATE T_USER SET LOGIN_NAME = #{loginName}," +
                "<if test=\" loginPassMD5 != null and loginPassMD5 != '' \">"+
                "LOGIN_PASS = #{loginPassMD5}," +
                "</if>"+
                "USER_ACCESS = #{codeValue} " +
            "WHERE USER_ID = #{id}" +
            "</script>")
    void updateUserInfo(JSONObject params);

    @Select("<script>SELECT a.CODE_VALUE codeValue, a.CODE_NAME codeName FROM T_SYS_CODE a WHERE a.CODE_TYPE='用户权限' ORDER BY a.CODE_VALUE</script>")
    List<User> findUsersAccess();

    @Select("<script>SELECT a.APP_ID appId,a.APP_NAME appName FROM T_APP a</script>")
    List<App> findAppsOfAllot();

    @Select("<script>SELECT a.APP_ID appId FROM T_APP a WHERE a.APP_ID IN (SELECT APP_ID FROM T_USER_APP WHERE USER_ID = #{userId})</script>")
    List<String> findAppsOfAllotByUser(String userId);

    @Delete("<script>DELETE FROM T_USER_APP WHERE USER_ID = #{id}</script>")
    void deleteAppsOfAllotByUser(String userId);

    @Insert("<script>" +
            "INSERT INTO T_USER_APP (USER_ID,APP_ID)\n" +
            "<foreach collection =\"list\" item=\"userApp\" separator =\"union all\">\n" +
            "\t\t (select\n#{userApp.userId},#{userApp.appId} from dual )\n" +
            "</foreach >" +
            "</script>")
    void addAppsOfAllotByUser(List<Map<String, String>> list);

    @Delete("<script>DELETE FROM T_USER_APP WHERE USER_ID = #{userId}</script>")
    void deleteRelevancy(String userId);
}
